package icu.lookyousmileface.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author starrysky
 * @title: CallableUse
 * @projectName Juc_Pro
 * @description: Callable代替了Runable接口，run/call，有异常有返回值
 * @date 2021/1/291:43 下午
 */
public class CallableUse {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //适配类
        FutureTask<Integer> integerFutureTask = new FutureTask<>(new MyThread());
        new Thread(integerFutureTask,"A").start();
        new Thread(integerFutureTask,"B").start();//有缓存，效率高
        //有返回值，get会产生阻塞，可以使用异步线程通信长处理
        System.out.println(integerFutureTask.get());
    }
}

class MyThread implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println("调用了call方法");
        return 1024;
    }
}
